//
//  FaceViewController.m
//  ATMCash
//
//  Created by zhengchengfeng on 2024/9/12.
//

#import "FaceViewController.h"
#import "IDVerfitySuccessController.h"
#import "PersonVertifyController.h"
#import "WorkInfoVertifyController.h"
#import "ContactVertifyController.h"
#import "BankVertifyController.h"

#define kFootViewHeight  (15 + 48 + (IS_IPhoneX_All ? kHeight_BottomSafe : 15))

@interface FaceViewController ()

@property (nonatomic) UIImageView *cardImgView;
@property (nonatomic) NSString *iconStr,*clickTimeStr;

@end

@implementation FaceViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)back{
    TipsAlertWLView *wl = [[TipsAlertWLView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [[UIApplication sharedApplication].keyWindow addSubview:wl];
    wl.clickSureBlock = ^{
        [[self getCurrentVC] jumpToPopControlClass:@"OrderDetailViewController"];
    };
}

- (void)initFootView{

    UIView *bottomV = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kFootViewHeight - kNavigationBarHeight, kScreenWidth, kFootViewHeight)];
    bottomV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomV];
    
    
    UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 15, kScreenWidth - 30, 48)];
    nextBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:16];
    [nextBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateSelected];
    [nextBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateHighlighted];
    [nextBtn setTitle:@"Next Step" forState:UIControlStateNormal];
    [nextBtn setTitle:@"Next Step" forState:UIControlStateSelected];
    [nextBtn setTitle:@"Next Step" forState:UIControlStateHighlighted];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateNormal];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateSelected];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateHighlighted];
    [nextBtn addTarget:self action:@selector(clickNextBtn) forControlEvents:UIControlEventTouchUpInside];
    [bottomV addSubview:nextBtn];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Verify Identity";
    
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kFootViewHeight );
    
    [self initFootView];
    

    UIImage *bgImg = [UIImage imageNamed:@"icon_idf"];
    UIImage *bgImg2 = [UIImage imageNamed:@"icon_idf18"];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, bgImg.size.height + DimensionBaseIphone6(bgImg2.size.height) + 10 )];
    self.tableView.tableHeaderView = headView;
    
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, bgImg.size.height)];
    imgV.image = bgImg;
    [headView addSubview:imgV];
    
    UIImageView *backImgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(15, imgV.bottom + 10, kScreenWidth - 30, DimensionBaseIphone6(bgImg2.size.height))];
    backImgView2.image = bgImg2;
    [headView addSubview:backImgView2];
    
    UIImage *ic1img = [UIImage imageNamed:@"icon_idf5"];
    UIImageView *ic1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, backImgView2.size.width, DimensionBaseIphone6(ic1img.size.height))];
    ic1.image = ic1img;
    [backImgView2 addSubview:ic1];
    
    UIImage *ic2img = [UIImage imageNamed:@"icon_idf3"];
    UIImageView *ic2 = [[UIImageView alloc] initWithFrame:CGRectMake((ic1.size.width - DimensionBaseIphone6(ic2img.size.width))/2, (ic1.size.height - DimensionBaseIphone6(ic2img.size.height))/2, ic2img.size.width, DimensionBaseIphone6(ic2img.size.height))];
    ic2.image = ic2img;
    [backImgView2 addSubview:ic2];
    
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, backImgView2.size.width/2, ic1.size.height)];
    [btn1 setTitle:[NSString stringWithFormat:@"  %@",esString(self.cardStr)] forState:UIControlStateNormal];
    [btn1 setTitle:[NSString stringWithFormat:@"  %@",esString(self.cardStr)] forState:UIControlStateSelected];
    [btn1 setTitle:[NSString stringWithFormat:@"  %@",esString(self.cardStr)] forState:UIControlStateHighlighted];
    [btn1 setImage:[UIImage imageNamed:@"icon_idf1"] forState:UIControlStateNormal];
    [btn1 setImage:[UIImage imageNamed:@"icon_idf1"] forState:UIControlStateSelected];
    [btn1 setImage:[UIImage imageNamed:@"icon_idf1"] forState:UIControlStateSelected];
    [btn1 setTitleColor:[UIColor colorWithHexString:@"#FFEFC0"] forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor colorWithHexString:@"#FFEFC0"] forState:UIControlStateSelected];
    [btn1 setTitleColor:[UIColor colorWithHexString:@"#FFEFC0"] forState:UIControlStateHighlighted];
    btn1.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
    [backImgView2 addSubview:btn1];

    
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(btn1.right, 0, backImgView2.size.width/2, ic1.size.height)];
    [btn2 setTitle:[NSString stringWithFormat:@"  %@",@"Face"] forState:UIControlStateNormal];
    [btn2 setTitle:[NSString stringWithFormat:@"  %@",@"Face"] forState:UIControlStateSelected];
    [btn2 setTitle:[NSString stringWithFormat:@"  %@",@"Face"] forState:UIControlStateHighlighted];
    [btn2 setImage:[UIImage imageNamed:@"icon_idf2-2"] forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:@"icon_idf2-2"] forState:UIControlStateSelected];
    [btn2 setImage:[UIImage imageNamed:@"icon_idf2-2"] forState:UIControlStateSelected];
    btn2.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
    [btn2 setTitleColor:[UIColor colorWithHexString:@"#F1F3F2"] forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor colorWithHexString:@"#F1F3F2"] forState:UIControlStateSelected];
    [btn2 setTitleColor:[UIColor colorWithHexString:@"#F1F3F2"] forState:UIControlStateHighlighted];
    [backImgView2 addSubview:btn2];
    
    UIImage *ic3 = [UIImage imageNamed:@"icon_idf6"];
    UIImageView *ic3imgV = [[UIImageView alloc] initWithFrame:CGRectMake(27, ic1.bottom + DimensionBaseIphone6(14), (backImgView2.size.width - 54), DimensionBaseIphone6(195))];
    ic3imgV.image = ic3;
    [backImgView2 addSubview:ic3imgV];
    
    UIImage *ic4 = [UIImage imageNamed:@"icon_idf8"];
    UIImageView *ic4imgV = [[UIImageView alloc] initWithFrame:CGRectMake(14, ic3imgV.bottom + DimensionBaseIphone6(20), (backImgView2.size.width - 28), DimensionBaseIphone6(ic4.size.height))];
    ic4imgV.image = ic4;
    [backImgView2 addSubview:ic4imgV];
    
    
    UIImage *ic5 = [UIImage imageNamed:@"icon_idf19"];
    self.cardImgView = [[UIImageView alloc] initWithFrame:CGRectMake(21, DimensionBaseIphone6(16), ic3imgV.size.width - 42, DimensionBaseIphone6(ic5.size.height))];
    [ic3imgV addSubview:self.cardImgView];
    self.cardImgView.image = ic5;
    
    ic3imgV.userInteractionEnabled = YES;
    backImgView2.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPhoto)];
    [ic3imgV addGestureRecognizer:tap];
    
}

- (void)clickPhoto{
    [self.view endEditing:YES];
    if (![StringUtil isCanUseCamera]) {
        return;
    }
    self.clickTimeStr = [NSString getNowTimeTimestamp:@"yyyyMMdd HH:mm:ss"];
    [[ZZYPhotoHelper shareHelper] showImageViewSelcteWithResultBlock:^(id data) {
        UIImage *imm = (UIImage *)data;
        NSData *imgData = UIImageJPEGRepresentation(imm, 0.5);
        
        NSString *path_document = NSHomeDirectory();
        
        NSString *imgPath = [path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@.png",[NSString getNowTimeTimestamp:@"yyyy-MM-dd HH:mm:ss"]]];
       
        [UIImagePNGRepresentation(imm) writeToFile:imgPath atomically:YES];
        
        NSMutableDictionary *muDic = @{
            @"sneering":@"2",
            @"gardens":esString(self.idStr),
            @"risen":@"10",
            @"smiled":imgPath,
            @"giggling":esString(self.cardStr),
            @"passable":@"",
            @"clearly":@"clearlyUploadFace",
            @"shell":@"1",
        }.mutableCopy;
        [CreaditUtilsHelper boxShowLoadWithMessage:@""];
        [CreditServerHelper postCreditPhotoUrl:kUploadPicUrl params:muDic imageData:imgData name:@"smiled" fileName:@"fileName.jpg" mimeType:@"image/jpeg" withProgress:^(double pro) {
            
        } success:^(id response) {
            [CreaditUtilsHelper hideLoadMessageBox];
            DLog(@"====response==%@",response);
            if ([response[@"dim"] integerValue] != kSuccessCode) {
                [CreaditUtilsHelper boxShowErrorWithMessage:esString(response[@"platform"]) duration:2.5f];
                return;
            }
            NSDictionary *dic = response[@"recollecting"];
            
            self.iconStr = esString(dic[@"exulting"]);
            
            [self.cardImgView sd_setImageWithURL:[NSURL URLWithString:self.iconStr]];
        
            [self.tableView reloadData];
            
            [CreaditUtilsHelper boxShowWithMessage:esString(response[@"platform"]) duration:2.50f];
        
//            [[NSNotificationCenter defaultCenter] postNotificationName:kNeedRefreshOrderDetNotification object:nil];
            
            [self enterNextPage];
            
            [CreditServerHelper uploadMaiDianDataStartTimeStr:self.clickTimeStr andEndTimeStr:[NSString getNowTimeTimestamp:@"yyyyMMdd HH:mm:ss"] withTypeStr:@"4" withFirmly:esString(self.idStr) withSmaller:esString([[NSUserDefaults standardUserDefaults] valueForKey:kOrderNumber])];
            
        } failure:^(NSError *error) {
            [CreaditUtilsHelper hideLoadMessageBox];
        }];
        
    
    } withOnlyCamera:YES andDefaultFront:YES];
    
  
    
}



- (void)clickNextBtn{
    [self clickPhoto];
    return;
    if (PMStringIsEmpty(self.iconStr)) {
        [CreaditUtilsHelper boxShowWithMessage:@"Please Upload Aadhaar Front"];
        return;
    }
    [self enterNextPage];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)enterNextPage{
    
    [CreaditUtilsHelper boxShowLoadWithMessage:@""];
    NSMutableDictionary *muDic = @{
        @"gardens":esString(self.idStr),
        @"tuesday":@"tuesdayOrderDet",
        @"needn":@"neednOrderDet",
    }.mutableCopy;
    [BaseDataEngine apiForPath:kProductDetUrl method:kPostMethod parameter:muDic onCompleteSuccess:^(NSDictionary *responseDic) {
        [CreaditUtilsHelper hideLoadMessageBox];
        DLog(@"=====%@",responseDic);
        if ([responseDic[@"dim"] integerValue] != kSuccessCode) {
            [CreaditUtilsHelper boxShowErrorWithMessage:esString(responseDic[@"platform"]) duration:2.5f];
            return;
        }
        
        NSDictionary *dic = responseDic[@"recollecting"];
       
        NSDictionary *lauDic = dic[@"laughing"];
        
        if (PMObjectIsEmpty(lauDic)) {
            [self enterOrderH5Page];
            return;
        }
        if ([esString(lauDic[@"tumbled"]) isEqualToString:kMakemoneyg]) {
            [self goPersonalVertify];
        }
        else if ([esString(lauDic[@"tumbled"]) isEqualToString:kMakemoneyh]) {
            [self goWorkVertify];
        }
        else if ([esString(lauDic[@"tumbled"]) isEqualToString:kMakemoneyi]) {
            [self goContactVertify];
        }
        else if ([esString(lauDic[@"tumbled"]) isEqualToString:kMakemoneyj]) {
            [self goBankVertify];
        }
        
    } onCompleteFailur:^(NSError *error) {
        [CreaditUtilsHelper hideLoadMessageBox];
    } withIsForm:YES];
    
}

- (void)enterOrderH5Page{
    NSString *stTime = [NSString getNowTimeTimestamp:@"yyyyMMdd HH:mm:ss"];
    NSMutableDictionary *muDic = @{
        @"commissariat":esString([[NSUserDefaults standardUserDefaults] valueForKey:kOrderNumber]),
        @"tracked":@"trackedIdStr",
        @"rot":@"rotIdStr",
        @"thrown":@"thrownIdStr",
        @"object":@"objectIdStr",
    }.mutableCopy;
    [CreaditUtilsHelper boxShowLoadWithMessage:@""];
    [BaseDataEngine apiForPath:kByOrderIdGetUrl method:kPostMethod parameter:muDic onCompleteSuccess:^(NSDictionary *responseDic) {
        [CreaditUtilsHelper hideLoadMessageBox];
        if ([responseDic[@"dim"] integerValue] != kSuccessCode) {
            [CreaditUtilsHelper boxShowErrorWithMessage:esString(responseDic[@"platform"]) duration:2.5f];
            return;
        }
        [CreditServerHelper uploadMaiDianDataStartTimeStr:stTime andEndTimeStr:[NSString getNowTimeTimestamp:@"yyyyMMdd HH:mm:ss"] withTypeStr:@"9" withFirmly:esString(self.idStr) withSmaller:esString([[NSUserDefaults standardUserDefaults] valueForKey:kOrderNumber])];
        NSDictionary *dic = responseDic[@"recollecting"];
        
        [[CreditCore globleCore] jumpToPage:esString(dic[@"exulting"])];
        
    } onCompleteFailur:^(NSError *error) {
        [CreaditUtilsHelper hideLoadMessageBox];
    } withIsForm:YES];
}

#pragma mark ----personalVertify
- (void)goPersonalVertify{
    PersonVertifyController *vc = [[PersonVertifyController alloc] init];
    vc.idStr = esString(self.idStr);
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ----workVertify
- (void)goWorkVertify{
    WorkInfoVertifyController *vc = [[WorkInfoVertifyController alloc] init];
    vc.idStr = esString(self.idStr);
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ----contactVertify
- (void)goContactVertify{
    ContactVertifyController *vc = [[ContactVertifyController alloc] init];
    vc.idStr = esString(self.idStr);
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ----bankVertify
- (void)goBankVertify{
    BankVertifyController *vc = [[BankVertifyController alloc] init];
    vc.idStr = esString(self.idStr);
    [self.navigationController pushViewController:vc animated:YES];
}

@end
